package logic

import (
	"context"
	"encoding/json"
	"shang/cache"
	"shang/models/mysql"
	"time"

	"shang/shangrpc/internal/svc"
	"shang/shangrpc/shangrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShangShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShangShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShangShowLogic {
	return &ShangShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShangShowLogic) ShangShow(in *shangrpc.ShangShowRequest) (*shangrpc.ShangShowResponse, error) {
	// todo: add your logic here and delete this line
	var shang mysql.Shang
	mysql.DB.Debug().Where("id=?", in.ShangId).Find(&shang)
	marshal, _ := json.Marshal(&shang)
	cache.Rdb.Set(l.ctx, "ShangShow", marshal, time.Minute*120)

	return &shangrpc.ShangShowResponse{
		Title:   shang.Title,
		Image:   shang.Image,
		Price:   float32(shang.Price),
		ClassId: int64(shang.ClassId),
	}, nil
}
